package design.pattern.Lu19VisitorPattern;

/**
 * @program JavaBase
 * @description:
 * @author: zhanglu
 * @create: 2020-05-07 23:55:00
 */
public class VisitorPattern06 {

    public static void main(String[] args) {
        AbsActor absActor = new OldActor();

        absActor.act((Role)new KungFuRole());   //静态绑定
        Role role = new KungFuRole();
        absActor.act(role);

        absActor.act(new KungFuRole()); //动态绑定

        //以上代码有问题，容易写错

        //如下优化：
        System.out.println("优化后...");
        AbsActor2 absActor2 = new OldActor2();

        Role2 role2 = new KungFuRole2();

        role2.accept(absActor2);


    }

}

interface Role{

}
class KungFuRole implements Role{

}
class IdiotRole implements Role{

}
abstract class AbsActor{
    public void act(Role role){
        System.out.println("演员可以扮演任何角色");
    }
    public void act(KungFuRole role){
        System.out.println("演员都可以扮演功夫角色");
    }
}
class YongActor extends AbsActor{
    public void act(KungFuRole role){
        System.out.println("最喜欢演功夫角色");
    }
}
class OldActor extends AbsActor {
    public void act(KungFuRole role) {
        System.out.println("年龄大了，不能演功夫角色");
    }
}

/******优化******/

interface Role2{
    void accept(AbsActor2 absActor);
}
class KungFuRole2 implements Role2{

    @Override
    public void accept(AbsActor2 absActor) {
        absActor.act(this);
    }
}
class IdiotRole2 implements Role2{

    @Override
    public void accept(AbsActor2 absActor) {
        absActor.act(this);
    }
}
abstract class AbsActor2{
    public void act(Role2 role){
        System.out.println("演员可以扮演任何角色");
    }
    public void act(KungFuRole2 role){
        System.out.println("演员都可以扮演功夫角色");
    }
}
class YongActor2 extends AbsActor2{
    public void act(KungFuRole2 role){
        System.out.println("最喜欢演功夫角色");
    }
}
class OldActor2 extends AbsActor2 {
    public void act(KungFuRole2 role) {
        System.out.println("年龄大了，不能演功夫角色");
    }
}